package com.share.domain.award.service.distribute.impl;

import com.share.domain.award.model.aggregate.GiveOutPrizeAggregate;
import com.share.domain.award.model.entity.DistributeAwardEntity;
import com.share.domain.award.model.entity.UserAwardRecordEntity;
import com.share.domain.award.model.entity.UserCreditAwardEntity;
import com.share.domain.award.model.valobj.AwardStateVo;
import com.share.domain.award.repository.IAwardRepository;
import com.share.domain.award.service.distribute.IDistributeAward;
import com.share.types.common.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.MathContext;

/**
 * 随机积分奖励发放服务
 */
@Component("user_credit_random")
public class UserCreditRandomAward implements IDistributeAward {

    @Resource
    private IAwardRepository awardRepository;


    @Override
    public void giveOutPrize(DistributeAwardEntity distributeAwardEntity) {
        Integer awardId = distributeAwardEntity.getAwardId();
        String awardConfig = distributeAwardEntity.getAwardConfig();
        if(StringUtils.isBlank(awardConfig)){
            //awardConfig为空，从数据库中查询
            awardConfig = awardRepository.queryAwardConfig(awardId);
        }
        String[] creditRange = awardConfig.split(Constants.SPLIT);
        if(creditRange.length!=2){
            throw new RuntimeException("award_config 「" + awardConfig + "」配置不是一个范围值，如 1,100");
        }
        BigDecimal creditAmount = generateRandom(new BigDecimal(creditRange[0]), new BigDecimal(creditRange[1]));

        //构建聚合对象
        UserAwardRecordEntity userAwardRecordEntity = GiveOutPrizeAggregate.buildUserAwardRecordEntity(
                distributeAwardEntity.getUserId(),
                distributeAwardEntity.getOrderId(),
                distributeAwardEntity.getAwardId(),
                AwardStateVo.complete);

        UserCreditAwardEntity userCreditAwardEntity = GiveOutPrizeAggregate.buildUserCreditAwardEntity(
                distributeAwardEntity.getUserId(),
                creditAmount);

        GiveOutPrizeAggregate giveOutPrizeAggregate = new GiveOutPrizeAggregate();
        giveOutPrizeAggregate.setUserId(distributeAwardEntity.getUserId());
        giveOutPrizeAggregate.setUserCreditAwardEntity(userCreditAwardEntity);
        giveOutPrizeAggregate.setUserAwardRecordEntity(userAwardRecordEntity);

        //存储发奖对象
        awardRepository.saveGiveOutPrizesAggregate(giveOutPrizeAggregate);

    }

    private BigDecimal generateRandom(BigDecimal min,BigDecimal max){
        if(min.equals(max)){
            return min;
        }
        /**
         * 随机积分计算：
         * Math.random()生成0-1的随机因子 比如0。5
         * max.substract(min)最小值到最大值的范围 比如 20-10 = 10
         * multiply相乘 0.5*10 = 5
         * min.add 在最小值上加上 5 即15 得到最终随机积分
         */
        BigDecimal randomCredit = min.add(BigDecimal.valueOf(Math.random()).multiply(max.subtract(min)));
        return randomCredit.round(new MathContext(3));
    }
}
